/**
 * @FileName: PromotionController.java
 * @Author
 * @Description:
 * @Date 2016年6月27日 下午2:31:12
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.controller.admin.garrison;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.controller.admin.BaseAdminController;
import cn.hpclub.server.model.garrison.GarrisonInfo;
import cn.hpclub.server.service.garrison.GarrisonService;
import cn.hpclub.server.util.ExcelRenderUtil;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.util.ObjectUtil;

/**
 * @FileName: GarrisonController.java
 * @Author zhl
 * @Description:驻点服务后台Controller
 * @Date 2016年6月27日 下午2:38:57
 * @CopyRight ZTE Corporation
 */
public class GarrisonController extends BaseAdminController<GarrisonInfo>{

    private static Logger logger = LoggerFactory.getLogger(GarrisonController.class);

    public void count(){
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        List<Record> list = new GarrisonService().service.getGarrisonCount(searchInfo);
        setAttr("items", list);
        render("/admin/garrison_count.tpl");
    }

    public void list(){
        List<Record> list = InterfaceUtil.getAllCity();
        setAttr("city_list", list);// 加载地市列表
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        int city_id = Integer.parseInt(getPara("city_id", "-1"));
        if(city_id == -1){
            int org_id = InterfaceUtil.getOrgId();
            Record record = InterfaceUtil.getInfoByOrg(org_id);
            if(ObjectUtil.isNotEmpty(record) && ObjectUtil.isNotEmpty(record.getInt("city_id"))){
                city_id = record.getInt("city_id");
            }
        }
        if((!searchInfo.contains("city_id") || "-1".equals(searchInfo.get("city_id"))) && city_id != -1){
            searchInfo.put("city_id", String.valueOf(city_id));
        }
        if("-1".equals(searchInfo.get("city_id"))){
            searchInfo.remove("city_id");
        }
        int city_id_attr = city_id;
        if(StrKit.notBlank(searchInfo.get("city_id"))){
            city_id_attr = Integer.parseInt(searchInfo.get("city_id"));
        }
        setAttr("city_id", city_id_attr);
        findByPageForSearch("SELECT * ", new GarrisonService().service.getSqlExceptSelect(searchInfo));
        render("/admin/garrison_detail.tpl");
    }

    public void exportAll(){
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        if("-1".equals(searchInfo.get("city_id"))){
            searchInfo.remove("city_id");
        }
        SXSSFWorkbook workbook = new GarrisonService().service.getExcel(searchInfo);
        render(new ExcelRenderUtil(workbook, "驻点服务"));
    }
}
